package com.modai.controller;

import com.modai.dto.CommonResponse;
import com.modai.entity.User;
import com.modai.service.UserService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import static com.modai.dto.CommonResponse.STATUS.SUCCESS;

/**
 * @author ：modai
 * @date ：Created in 2021-6-5 18:21
 * @description：用户控制器
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @SneakyThrows
    @GetMapping("/{userId}")
    public CommonResponse<User> getUser( @PathVariable Integer userId){
        User user = userService.retrieveUser(userId);
        return new CommonResponse<>(SUCCESS,user);
    }

    @PostMapping
    public CommonResponse<String> insertUser(@RequestBody @Validated User user){
        User userNow = userService.createUser(user);
        return new CommonResponse<>(SUCCESS,userNow.getName()+": add success");
    }

    @PutMapping
    public CommonResponse<String> updateUser(@RequestBody User user){
        User userNow = userService.updateUser(user);
        return new CommonResponse<>(SUCCESS,userNow.getName()+": update success");
    }

    @DeleteMapping
    public CommonResponse<String> deleteUser(@RequestParam(value = "userId") Integer userId){
        User userNow = userService.deleteUser(userId);
        return new CommonResponse<>(SUCCESS,userNow.getName()+": delete success");

    }

    @GetMapping("/userNum")
    public CommonResponse<Integer> getUserNum(){
        return new CommonResponse<>(SUCCESS, userService.getUserNum());
    }

}
